<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var numSubarray = function (nums, left, right) {

      const cnt = max => {
        // 如何计算最大值小于等于max的子数组个数?
        // 枚举以每个数字结尾的子数组
        //                                  最大值              满足的子数组个数
        // 以2结尾的子数组    2                  1                     1        
        // 以1结尾的子数组    2 21               2                     1+2
        // 以4结尾的子数组    4 14 214           4
        // 以3结尾的子数组    3 43 143 2143      4  
        let cur = 0, res = 0
        for (let x of nums) {
          if (x <= max) {
            res += ++cur
          } else cur = 0
        }
        return res
      }
      return cnt(right) - cnt(left - 1)
    };
    console.log(numSubarray(nums = [2, 1, 4, 3], left = 2, right = 3))
  </script>
</body>

</html> 